package oddments;

import java.util.regex.*;

import processing.core.PApplet;

public class ParseSize extends PApplet{

	// The pattern is used to find the width and height from the size() statement
	// I have shamelessly taken this code from ProcessingJS by florian jenett
	// because it is way beyond my knowledge of regular expressions.
	String SK_SIZE = "(?:^|\\s|;)size\\s*\\(\\s*(\\S+)\\s*,\\s*(\\d+),?\\s*([^\\)]*)\\s*\\)";

	Pattern p; // = Pattern.compile(regex);
	Matcher m; // = p.matcher(sizes);

	String[] sizes = {
			"public void size(480,320) ;",
			"public  void  size  ( 480 , 320*2 ) ;",
			"void size(480, 320, OPENGL);",
			"void  size   (  480 ,   320  ,P3D);",
			"void    size(400, 400, PDF, \"filename.pdf\");"
	};

	public void setup(){
		p = Pattern.compile(SK_SIZE);
		for(String s : sizes)
			performRegex(s, p);
		exit();
	}

	public void performRegex(String s, Pattern p) {
		m = p.matcher(s);
		if (m.find()) {
			System.out.print("["+m.groupCount ()+ "] ");
			for (int i = 0; i<= m.groupCount (); i++) 
				System.out.print("'" + m.group(i) + "'  ");
			println();
		} else {
			System.out.println("No match found for '" + s + "'");
		}
		println("----------------------------------------------------");
	}
}
